Ext.define('StaffServer.controller.BatchLogController', {
	extend : 'Ext.app.Controller',

	models: ['BatchLogModel'],
	stores: ['BatchLogStore'],
	views:  ['batchLog.BatchLogGrid', 'batchLog.BatchLogMngr', 'batchLog.BatchLogSearchPanel'],
	
	tabView: 'StaffServer.view.batchLog.BatchLogMngr',
	
	
	refs : [ {
		ref : 'batchLogSearchPanel',
		selector : 'batchLogSearchPanel'
	}, {
		ref : 'batchLogGrid',
		selector : 'batchLogGrid'
	}],

	// 初始化执行语句
	init : function() {
		// 控制事件
		this.control({
			'batchLogSearchPanel button[action=search]' : {
				click : this.search
			},
			'batchLogSearchPanel button[action=reset]' : {
				click : this.reset
			},
			'batchLogGrid button[action=executeBatch]' : {
				click : this.executeBatch
			}
		});
	},

	//searchPanel查询
	search : function() {
		var searchPanel = this.getBatchLogSearchPanel();
		var grid = this.getBatchLogGrid();
		grid.store.on('beforeload', function(store){
			var searchParams = searchPanel.getForm().getFieldValues();
			Ext.apply(store.proxy.extraParams, searchParams);
        });
		Ext.ComponentQuery.query('batchLogGrid > pagingtoolbar')[0].moveFirst();
	},
	
	//searchPanel重置
	reset: function(){
		var searchPanel = this.getBatchLogSearchPanel();
		searchPanel.getForm().reset();
	},
	
	//批处理
	executeBatch: function(){
		var $this = this;
        Ext.MessageBox.confirm('请确认', '确认执行批处理吗?', function(btn, text){
            if (btn == 'yes') {
                showWaitMsg(); 
                Ext.Ajax.request({
                    url: 'batchLog/executeBatch',
                    success: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        if (resp.success) {
                            Ext.Msg.alert('提示', '操作成功', function(){
                            	$this.search();
                            });
                        }
                        else {
                            Ext.Msg.alert('提示', resp.error);
                        }
                    },
                    failure: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        Ext.Msg.alert('提示', resp.error);
                    }
                });
            }
        });
	}
	
});